package com.study.amqp;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.Exchange.DeleteOk;
import com.rabbitmq.client.AMQP.Queue.BindOk;
import com.rabbitmq.client.AMQP.Queue.DeclareOk;
import com.rabbitmq.client.AMQP.Queue.UnbindOk;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 创建RabbitMQ的核心元素,exchange,queue,以及对二者进行绑定和解绑
 *
 */
public class App {
	public static void main(String[] args) throws IOException, TimeoutException {
		ConnectionFactory factory = new ConnectionFactory();
		// 配置连接信息
		factory.setHost("127.0.0.1");
		factory.setPort(5672);
		factory.setUsername("guest");
		factory.setPassword("guest");
		factory.setVirtualHost("/");
		//创建连接，并通过连接创建通道
		Connection conn = factory.newConnection();
		Channel channel = conn.createChannel();
		
		//使用通道创建exchange
		//DeclareOk ok=channel.exchangeDeclare("log", BuiltinExchangeType.DIRECT, true, false, false, null);
		//System.out.println(ok);

		//使用通道创建queue
//		DeclareOk queueOk=channel.queueDeclare("java_queue", true, false, false, null);
//		System.out.println(queueOk);
		
		//绑定exchange和queueu
//		BindOk ok=channel.queueBind("java_queue", "log", "info");
//		System.out.println(ok);
		
		//解绑 exchange 和queue
//		UnbindOk ok=channel.queueUnbind("java_queue", "log", "info");
//		System.out.println(ok);
		
		//判断exchange 是否存在
//		DeclareOk ok= channel.exchangeDeclarePassive("log");
//		System.out.println(ok);
		
		//判断queue是否存在
//		DeclareOk ok=channel.queueDeclarePassive("java_queue");
//		System.out.println(ok);
		
		
//		//删除queue
//		DeleteOk ok=channel.queueDelete("java_queue");
//		System.out.println(ok);
		
		//删除exchange
//		DeleteOk ok=channel.exchangeDelete("log");
//		System.out.println(ok);
		
		/**以上的方法都有对应的无返回类型的方法（异步的方法）**/
		
		// 关闭通道和连接
		channel.close();
		conn.close();

	}
}
